#include "DynamicArray1.h"

struct Student
{
    Student(int id,const std::string& name):id(id),name(name)
    {
        std::cout<<"构造函数！\n";
    }
    ~Student()
    {
        std::cout<<"析构函数！\n";
    }
    int id;
    std::string name;
};

void ShowStudent(void *p)
{
    Student *s = (Student *)p;
    std::cout<<s->id<<" "<<s->name<<std::endl;
}

void FreeStudent(void *p)
{
    Student *s = (Student *)p;
    delete s;
}

bool CompareID(void *p1,void *p2)
{
    Student *s1 = (Student *)p1;
    Student *s2 = (Student *)p2;
    return s1->id > s2->id;
}

//下次可以只需要写一个回调函数
bool CompareName(void *p1,void *p2)
{
    Student *s1 = (Student *)p1;
    Student *s2 = (Student *)p2;
    return s1->name > s2->name;
}

bool isNameEqual(void *p1,void *p2)
{
    Student *s1 = (Student *)p1;
    char *s = (char*)p2;
    return s1->name == s;
}

bool isIDEqual(void *p1,void *p2)
{
    Student *s1 = (Student *)p1;
    long id = (long)p2;
    return s1->id == id;
}

int main()
{
    // DynamicArray1 a;
    
    // // int a[] ={2,5,7,3,8,9};

    // for(int i = 0; i < 20; i++)
    // {
    //     a.InsertTail(i + 1);
    // }
    
    // a.InsertByIndex(100,1);//在第一个位置插入100
    // a.RemoveByIndex(1);//删除第一个位置
    // a.RemoveByElement(2);
    // a.UpDateByIndex(1,999);
    // a.UpDateByElement(1,999);
    
    // a[1] = 999;

    // for(int i = 0; i < 10; i++)
    // {
    //     a.InsertHead(i + 1);
    // }
    // // DynamicArray1 result = a.FindByElement(10);
    // // std::cout<<result;
   
    // a.Sort();
    // // std::cout<<a;
    // // std::cout<<a.BinarySearch(6)<<std::endl;
    // // std::cout<<a.BinarySearch(12)<<std::endl;
    // // std::cout<<a.BinarySearch(99)<<std::endl;


    // a.Deduplicate();
    // std::cout<<a;

    // DynamicArray1 a;
    // for(int i = 0 ; i < 10; i++)
    // {
    //     a.InsertTail(i + 1);
    // }
    // DynamicArray1 b;
    // for(int i = 0 ; i < 10; i++)
    // {
    //     b.InsertTail(i + 1);
    // }
    // std::cout<<a;
    // std::cout<<b;

    // DynamicArray1 c = a.Merge(b);
    // std::cout<<c;


    DynamicArray1 a(FreeStudent);
    a.InsertTail(new Student(2,"b"));
    a.InsertTail(new Student(3,"a"));
    a.InsertTail(new Student(1,"c"));
    a.show(ShowStudent);
    // std::cout<<"按id-------------\n";
    // a.SetSortCallBack(CompareID);//函数指针传给我
    // a.Sort();//定不下来的东西只能传回调来使用//在sort函数里面使用函数指针//调用的不是我写的函数，就是叫回调函数
    // a.show(ShowStudent);
    // std::cout<<"按name-------------\n";
    // a.SetSortCallBack(CompareName);
    // a.Sort();//调用排序
    // a.show(ShowStudent);

    std::cout<<"-------------\n";
    a.RemoveByElement((void*)1,isIDEqual);
    a.show(ShowStudent);


    return 0;
    
}